package com.yidu.t299.dao.twj;

import com.yidu.t299.domain.twj.SysUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface SysUserMapper {
    /**
     * 根据主键删除用户信息(实际上是修改flag字段的值，不能直接删除信息)
     * @param usrId 用户编号
     * @return 影响的行数(1:成功)
     */
    @Delete("UPDATE sys_user SET usr_flag = 0 WHERE usr_id = #{usrId}")
    int deleteByPrimaryKey(@Param("usrId") Integer usrId);

    /**
     * 根据主键批量删除用户信息(实际上是修改flag字段的值，不能直接删除信息)
     * @param array 用户编号数组
     * @return 影响的行数(1:成功)
     */
    @Delete("<script> " +
            "        UPDATE sys_user SET usr_flag = 0 WHERE usr_id in " +
            "        <foreach collection=\"array\" open=\"(\" close=\")\" separator=\",\" item=\"usrId\"> " +
            "            #{usrId,jdbcType=INTEGER} " +
            "        </foreach> " +
            "</script>")
    int deleteBatch(Integer[] array);

    /**
     * 添加用户信息
     * @param sysUser 用户对象
     * @return 影响的行数(1:成功)
     */
    @Insert("insert into sys_user (usr_id, usr_name, usr_password,usr_role_id, usr_flag, record_id ) " +
            " values (#{usrId,jdbcType=INTEGER}, #{usrName,jdbcType=VARCHAR}, #{usrPassword,jdbcType=VARCHAR}," +
            " #{usrRoleId,jdbcType=INTEGER}, #{usrFlag,jdbcType=INTEGER}, #{recordId,jdbcType=INTEGER}) ")
    int insert(SysUser sysUser);

    /**
     * 根据主键查询用户信息
     * @param usrId 用户编号
     * @return 用户对象
     */
    @Select("select usr_id, usr_name, usr_password, usr_role_id, usr_flag, record_id " +
            "    from sys_user " +
            "    where usr_id = #{usrId,jdbcType=INTEGER} ")
    @Results(id = "BaseResultMap",value = {
            @Result(id = true,column = "usr_id",property = "usrId",jdbcType = JdbcType.VARCHAR),
            @Result(column = "usr_name",property = "usrName",jdbcType = JdbcType.VARCHAR),
            @Result(column = "usr_password",property = "usrPassword",jdbcType = JdbcType.VARCHAR),
            @Result(column = "usr_role_id",property = "usrRoleId",jdbcType = JdbcType.VARCHAR),
            @Result(column = "usr_flag",property = "usrFlag",jdbcType = JdbcType.VARCHAR),
            @Result(column = "record_id",property = "recordId",jdbcType = JdbcType.VARCHAR)
    })
    SysUser selectByPrimaryKey(@Param("usrId") Integer usrId);


    /**
     * 根据用户名查询
     * 1、注册：用户名查重
     * 2、用户登录（精细化提示使用，先判断用户名，再判断密码）
     * @param usrName 用户名
     * @return 用户对象
     */
    @Select("select usr_id, usr_name, usr_password, usr_role_id, usr_flag, record_id " +
            "    from sys_user " +
            "    where usr_name = #{usrName,jdbcType=INTEGER} ")
    @ResultMap("BaseResultMap")
    SysUser selectByUsrName(String usrName);


    /**
     * 根据所给属性值来修改其字段值
     * @param sysUser 用户对象
     * @return 影响的行数(1:成功)
     */
    @Update("<script> " +
            "update sys_user\n" +
            "    <set>\n" +
            "      <if test=\"usrName != null\">\n" +
            "        usr_name = #{usrName,jdbcType=VARCHAR},\n" +
            "      </if>\n" +
            "      <if test=\"usrPassword != null\">\n" +
            "        usr_password = #{usrPassword,jdbcType=VARCHAR},\n" +
            "      </if>\n" +
            "      <if test=\"usrRoleId != null\">\n" +
            "        usr_role_id = #{usrRoleId,jdbcType=INTEGER},\n" +
            "      </if>\n" +
            "      <if test=\"usrFlag != null\">\n" +
            "        usr_flag = #{usrFlag,jdbcType=INTEGER},\n" +
            "      </if>\n" +
            "      <if test=\"recordId != null\">\n" +
            "        record_id = #{recordId,jdbcType=INTEGER},\n" +
            "      </if>\n" +
            "    </set>\n" +
            "    where usr_id = #{usrId,jdbcType=INTEGER} " +
            "</script> ")
    int update(SysUser sysUser);



    /**
     * 批量查询
     * @param sysUser 用户对象
     * @return 用户对象集合
     */
    @Select("<script> " +
            "   select usr_id, usr_name, usr_password, usr_role_id, usr_flag, record_id " +
            "   from sys_user " +
            "   <where>\n" +
            "            <if test=\"usrName != null and usrName != ''\">\n" +
            "                usr_name like concat('%',#{usrName},'%')\n" +
            "            </if>\n" +
            "   </where> " +
            "</script> ")
    @ResultMap("BaseResultMap")
    List<SysUser> select(SysUser sysUser);


}